<!-- 单图 -->
<template>
	<div>
		<el-upload class="avatar-uploader" :action="$store.state.baseUrl + 'cdc/api/common/upload'" :show-file-list="false" :on-success="handleAdd" :before-upload="beforeAvatarUpload" :headers="{ token: $store.state.token }">
			<img v-if="list.length&&list[0].url" :src="list[0].url" class="avatar">
			<i v-else class="el-icon-plus avatar-uploader-icon"></i>
		</el-upload>
	</div>
</template>
<script>
export default {
	name: 'upload',
	props: {
		dialogVisible: false,
		totalUp: false,
		list: Array,
		listType: {
			type: String,
			default: '',
		},
		uploadType: {
			type: String,
			default: '',
		},
		limit: {
			type: Number,
			default: 9999,
		},
		type: {
			type: String,
			default: 'upload', //upload 上传 look 查看
		},
		title: {
			type: String,
			default: '上传文件',
		},
	},
	data() {
		return {
			dialogImageUrl: '',
			imageUrl: '',
			dialogVisible4: false,
		};
	},
	methods: {
		handleExceed(files, fileList) {
			this.$message.warning(
				`当前限制上传 1 个文件，本次选择了 ${
					files.length
				} 个文件，文件总数 ${files.length + fileList.length} `
			);
		},
		handlePictureCardPreview(file) {
			console.log(file);
			if (file.url.indexOf('http') == -1) {
				//老数据转换
				file.url = this.$store.state.openUrl + this.lookUrl(file.url);
			}
			if (file.url.indexOf('pdf') != -1) {
				window.open(file.url);
			} else {
				this.dialogVisible4 = true;
				this.dialogImageUrl = file.url;
			}
		},
		lookUrl(url) {
			//木材老数据附件 转换格式
			var url_new = url.split('_');
			var url_type = url_new[0];
			if (url_new.length == 2) {
				var dir1 = url_new[1].substr(0, 4);
				var dir2 = url_new[1].substr(4, 2);
				var dir3 = url_new[1].substr(6, 2);
				return (
					'/Uploads/' +
					url_type +
					'/' +
					dir1 +
					'/' +
					dir2 +
					'/' +
					dir3 +
					'/' +
					url
				);
			}
		},
		handleRemove(file) {
			this.$emit('handleRemove', file);
		},
		handleAdd(response, file, fileList) {
			console.log('上传成功', response, file, fileList);
			// this.list =[{url:response.data[0].url}];
			this.$emit('handleAdd', fileList, response);
		},
		batchSubmit() {
			console.log('全量提交');
			this.$emit('batchSubmit');
		},
		closeDialog() {
			console.log('close', this.title);
			this.$emit('close');
		},
		beforeAvatarUpload(file) {
			console.log(file)
			const isJPG = file.type === 'image/jpeg';
			const isPNG = file.type === 'image/png';
			const isLt2M = file.size / 1024 / 1024 < 2;
			if (!isJPG && !isPNG) {
				this.$message.error('上传头像图片只能是 JPG/PNG 格式!');
			}
			if (!isLt2M) {
				this.$message.error('上传头像图片大小不能超过 2MB!');
			}
			return (isJPG ||isPNG)  && isLt2M;
		},
	},
};
</script>
